﻿using Lsk.Business.DTO;
using Lsk.Data;
using System;
using System.Collections.Generic;
using System.Data.Entity.Core.Objects;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;

namespace Lsk.Business
{
    public class UserSubmitInfo
    {
        private lskEntities _entity;
     
        public UserSubmitInfo()
        {
            _entity = new lskEntities();
        }

        public IEnumerable<String> SaveBuyRent(BuyRentParameter search)
        {

            String name = search.name;
            String phone = search.phone; 
            String email = search.email;
            String company = search.company;
            String locationKeyword = search.locationKeyword;
            String propertyType= search.propertyType;  //Residential, Commercial, Land, Industrial
            String submissionType= search.submissionType; //Buy, Rent, All
            String useInvest = search.useInvest;  // Y/N
            String remark = search.remark;
            decimal price = search.price;
            IEnumerable<String> SaveBuyRentResult =  _entity.UserWantBuyRent(name, phone, email, company, locationKeyword, propertyType, submissionType, price, useInvest, remark);

            return SaveBuyRentResult;
        }

        public int SaveJobCandidate(JobCandidateParameter candidate)
        {

            String name = candidate.name;
            String handPhone = candidate.phone;
            String homeAddress = candidate.address;
            String icNo = candidate.ic;
            String gender = candidate.gender;
            String position = candidate.position;  

            int applicationStatus = _entity.InsertJobCandidate(name, handPhone, gender, icNo, homeAddress, position);
            string body = @"<html><body>
                            <div style = ""background-color: #ece8d4;width: 600px; height: 200px; padding: 30px; margin - top:30px; ""><p> Dear admin,<p>          
                            <p> User submits their info at penangrealty.com.</p>
                           
                             <br/>
                                <table>
                                    <tr>
                                        <td>  Name : </td> <td>  {0} </td>
                                    </tr>

                                    <tr>
                                        <td>  HandPhone : </td> <td>  {1} </td>
                                    </tr>

                                    <tr>
                                        <td>  Address : </td> <td>  {2} </td>
                                    </tr>

                                    <tr>
                                        <td>  IC : </td> <td>  {3} </td>
                                    </tr>
                                     <tr>
                                        <td>  Gender : </td> <td>  {4} </td>
                                    </tr>
                                    <tr>
                                        <td>  Position : </td> <td>  {5} </td>
                                    </tr>
                                    </table>             
                            <br/>
                            <p> Thank you! </p>
                           </div>
                           </body>
                           </html>";
            body = string.Format(body, name, handPhone, homeAddress, icNo, gender, position);
            try
            {
                SendEmail("Longsookeat@Lskworld.com", "Notification : New Submission to Join Your Team!", body);
            }
            catch (Exception ex)
            {

            }
            return applicationStatus;
        }

        public void SendEmail(string To, string Title, string Content)
        {
            Users u = new Users();
            EmailMessage message = new EmailMessage();
            message.Body = Content;
            message.Title = Title;
            message.Email = To;
            u.SendEmailMsg(message);

           // string smtpAddress = "smtp.gmail.com";
           // int portNumber = 587;
           // bool enableSSL = true;
           // string emailFrom = "lskpenangrealty@gmail.com";
           // string password = "Lsk1234%";
           // string emailTo = To;
           // string subject = Title;
           //string messageBody = Content;
           // using (MailMessage mail = new MailMessage())
           // {
           //     mail.From = new MailAddress(emailFrom);
           //     mail.To.Add(emailTo);
           //     mail.Subject = subject;
           //     mail.Body = messageBody;
           //     mail.IsBodyHtml = true;
           //     // Can set to false, if you are sending pure text.


           //     using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
           //     {
           //         smtp.Credentials = new NetworkCredential(emailFrom, password);
           //         smtp.EnableSsl = enableSSL;
           //         smtp.Send(mail);
           //     }
           // }


        }
    }
}
